package com.zzptc.easyshop.util;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MyUtil {
    /**
     * 读取配置文件xml
     *
     * @return
     * @throws Exception
     */
    public static Map<String, String> readXML(String filename) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        SAXBuilder saxBuilder = new SAXBuilder();
        String path = MyUtil.class.getResource("/").getPath() + filename;
        Document doc = saxBuilder.build(path);
        //得到根结点
        Element rootelment = doc.getRootElement();
        System.out.println("根结点：" + rootelment.getName());
        //得到子结点
        List<Element> listall = rootelment.getChildren();

        //循环    迭代
//        for(int i=0;i<listall.size();i++){
//            System.out.println("子结点"+i+":"+listall.get(i).getName());
//        }
        Iterator iter = listall.iterator();//拿到迭代器
        while (iter.hasNext()) {//迭代器有几个方法   判断有没有元素hasNext()   有元素就next()
            Element e = (Element) iter.next();
            // System.out.println(e.getName()+"**********"+e.getText());
            //放入map
            map.put(e.getName(), e.getText());
        }


        return map;
    }


    public static void main(String[] args) {
        try {
            System.out.println(MyUtil.readXML("db-config.xml").size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
